#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "Car.h"
#include "Parkinglot.h"

//创建一个停车场
ParkingLot* Park_Create() {
    ParkingLot* park = (ParkingLot*)malloc(sizeof(ParkingLot));
    for (int i = 0;i < MAX_CARS;i++) {
        park->spot[i] = NULL;
    }
    park->sum = 0;
    return park;
}

//判断停车场是否为空，是返回1，否则0
int Park_IsEmpty(ParkingLot* park) {
    int is = (park->sum == 0 ? 1 : 0);
    return is;
}

//判断停车场是否为满，是返回1，否则0
int Park_IsFull(ParkingLot* park) {
    int is = (park->sum == MAX_CARS ? 1 : 0);
    return is;
}



//输入车牌号搜索车位，若无则返回-1
int Park_Check(ParkingLot *park /*, char* plate*/) {
    char plate[PLATE_SIZE];
    printf("请输入车牌号");
    scanf("%s", plate);
    for (int i = 0;i < MAX_CARS;i++) {
        if (strcmp(park->spot[i]->license_plate, plate) == 0) {
            printf("位于车位%d", i);
            return i;
        }
    }
    printf("未找到车辆");
    return -1;
}

//输入车牌号，车辆进入停车场
ParkingLot* Park_Add(ParkingLot* park /*, char* plate*/ ) {
    if (!Park_IsFull(park)) {
        char plate[PLATE_SIZE];
        printf("请输入车牌号");
        scanf("%s", plate);
        for (int i = 0;i < MAX_CARS;i++)
            if (park->spot[i] == NULL) {
                park->spot[i] = Car_Create();
                strcpy(park->spot[i]->license_plate , plate);
                park->spot[i]->in_time = time(NULL);
                break;
            }
        
    }
    else printf("停车场已满");
    return park;

}

//扫描车牌，离开停车场
ParkingLot* Park_Leave(ParkingLot* park /*, char* plate*/ ) {
    char plate[PLATE_SIZE];
    printf("请输入车牌号");
    scanf("%s", plate);
    for (int i = 0;i < MAX_CARS;i++) {
        if (strcmp(park->spot[i]->license_plate , plate) == 0) {
            
            park->spot[i]->out_time = time(NULL);
            Car_Bill(park->spot[i]);
            /*输入历史数据的数据库
            
            */
            park->spot[i] == NULL;
            break;
        }
            
    }

    return park;
}

//打印目前停车场所有车辆信息
void Park_Allcheck(ParkingLot *park){
    for (int i = 0;i < MAX_CARS;i++) {
        if (park->spot[i] != NULL)
            Car_Check(park->spot[i]);
    }
    printf("输出完成");
}
